define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'litestoregoods'], function($, undefined, Backend, Table, Form, Template, litestoregoods) {

    var Controller = {
        index: function() {
            $(".btn-add").data("area", ["1000px", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoods/index',
                    add_url: 'litestore/litestoregoods/add',
                    edit_url: 'litestore/litestoregoods/edit',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    import_url: 'litestore/litestoregoods/import',    

                    table: 'litestore_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_id', title: __('Goods_id') },
                        {field: 'gongyingshang.name', title: __('供应商')},

                        { field: 'goods_name', title: __('Goods_name'), operate: 'LIKE' },
                        { field: 'desc', title: __('商品描述') },
                        { field: 'category.name', title: __('Category.name') },
                        { field: 'category_id', visible: false, title: __('Category_id') },
                        { field: 'image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'images', title: __('Images'), formatter: Table.api.formatter.images },
                        { field: 'spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },
                        // {field: 'deduct_stock_type', title: __('Deduct_stock_type'), searchList: {"10":__('Deduct_stock_type 10'),"20":__('Deduct_stock_type 20')}, formatter: Table.api.formatter.normal},
                        { field: 'freight.name', title: __('Freight.name') },
                        { field: 'sales_initial', title: __('Sales_initial') },
                        { field: 'sales_actual', title: __('Sales_actual') },
                        { field: 'goods_sort', title: __('Goods_sort') },
                        // {field: 'delivery_id', title: __('Delivery_id')},
                        { field: 'goods_status', title: __('Goods_status'), searchList: { "10": __('Goods_status 10'), "20": __('Goods_status 20') }, formatter: Table.api.formatter.status },
                        // {field: 'is_delete', title: __('Is_delete'), searchList: {"0":__('Is_delete 0'),"1":__('Is_delete 1')}, formatter: Table.api.formatter.normal},
                        { field: 'home_nav', title: __('首页推荐'), searchList: { "1": __('热销产品'), "2": __('新品上架'), "4": __('最近降价'), "4": __('特价专区') }, formatter: Table.api.formatter.normal },
                        { field: 'is_fangxin', title: __('是否放心购'), searchList: { "0": __('否'), "1": __('是') }, formatter: Table.api.formatter.normal },

                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["100%", "800px"]);
            });



            $(document).on("click", ".btn-selected", function() {

                let ids = Table.api.selectedids(table);

                var event = $(this).attr('event');
                var url = $(this).attr('data-url');
                url = url + '?ids=' + ids;
                // Layer.alert(JSON.stringify(Table.api.selecteddata(table)));

                Fast.api.open(url, '批量设置门店', {
                    area: ['60%', '70%'],
                    callback(selectData) {
                        // let check_user_ids = '';
                        // selectData.map(function (item, k) {
                        //     check_user_ids += check_user_ids ? ',' + item : item;
                        // });
                        // $(that).parent().parent().children('input[name="row[user_ids]"]').val(check_user_ids);
                        // $(that).parent().parent().parent().parent().find('.i-look').show();
                    }
                });


            });
            $("#btn-import-file").on('uploadsuccess', function (event, data) {
                    // 上传成功后返回的文件信息
                    var fileInfo = data.data;

                    // 再发请求到你的导入接口
                    Fast.api.ajax({
                        url: 'litestore/litestoregoods/import',
                        data: { file: fileInfo.url },
                    }, function (ret, res) {
                        Toastr.success('导入成功');
                        // 刷新表格
                        $(".btn-refresh").trigger("click");
                        return false;
                    });
                });    



        },


        index2: function() {
            $(".btn-add").data("area", ["100%", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoods/index2',
                    add_url: 'litestore/litestoregoods/add2',
                    edit_url: 'litestore/litestoregoods/edit2',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestore_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_id', title: __('Goods_id') },
                        { field: 'goods_name', title: __('Goods_name') },
                        { field: 'desc', title: __('商品描述') },
                        { field: 'category.name', title: __('Category.name') },
                        { field: 'category_id', visible: false, title: __('Category_id') },
                        { field: 'image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'images', title: __('Images'), formatter: Table.api.formatter.images },
                        { field: 'spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'deduct_stock_type', title: __('Deduct_stock_type'), searchList: { "10": __('Deduct_stock_type 10'), "20": __('Deduct_stock_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'freight.name', title: __('Freight.name') },
                        { field: 'sales_initial', title: __('Sales_initial') },
                        { field: 'sales_actual', title: __('Sales_actual') },
                        { field: 'goods_sort', title: __('Goods_sort') },
                        { field: 'delivery_id', title: __('Delivery_id') },
                        { field: 'goods_status', title: __('Goods_status'), searchList: { "10": __('Goods_status 10'), "20": __('Goods_status 20') }, formatter: Table.api.formatter.status },
                        { field: 'is_delete', title: __('Is_delete'), searchList: { "0": __('Is_delete 0'), "1": __('Is_delete 1') }, formatter: Table.api.formatter.normal },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["1000px", "800px"]);
            });

        },

        index3: function() {
            $(".btn-add").data("area", ["1000px", "800px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'litestore/litestoregoods/index3',
                    add_url: 'litestore/litestoregoods/add3',
                    edit_url: 'litestore/litestoregoods/edit3',
                    del_url: 'litestore/litestoregoods/del',
                    multi_url: 'litestore/litestoregoods/multi',
                    table: 'litestore_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_sort',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'goods_id', title: __('Goods_id') },
                        { field: 'goods_name', title: __('Goods_name') },
                        { field: 'desc', title: __('商品描述') },
                        { field: 'category.name', title: __('Category.name') },
                        { field: 'category_id', visible: false, title: __('Category_id') },
                        { field: 'image', title: __('封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'images', title: __('Images'), formatter: Table.api.formatter.images },
                        { field: 'spec_type', title: __('Spec_type'), searchList: { "10": __('Spec_type 10'), "20": __('Spec_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'deduct_stock_type', title: __('Deduct_stock_type'), searchList: { "10": __('Deduct_stock_type 10'), "20": __('Deduct_stock_type 20') }, formatter: Table.api.formatter.normal },
                        { field: 'freight.name', title: __('Freight.name') },
                        { field: 'max_kan', title: __('最多可砍金额') },
                        { field: 'sales_initial', title: __('Sales_initial') },
                        { field: 'sales_actual', title: __('Sales_actual') },
                        { field: 'goods_sort', title: __('Goods_sort') },
                        { field: 'delivery_id', title: __('Delivery_id') },
                        { field: 'goods_status', title: __('Goods_status'), searchList: { "10": __('Goods_status 10'), "20": __('Goods_status 20') }, formatter: Table.api.formatter.status },
                        { field: 'is_delete', title: __('Is_delete'), searchList: { "0": __('Is_delete 0'), "1": __('Is_delete 1') }, formatter: Table.api.formatter.normal },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function(data) {
                $(".btn-editone").data("area", ["1000px", "800px"]);
            });

        },



        add: function() {


            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });

            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            });

            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });

            const { reactive, computed, onMounted, getCurrentInstance, watch } = Vue
            const addEdit = {
                setup() {
                    const state = reactive({
                        options: Config.typeList,
                        props: { value: 'id', label: "name", children: "childlist" ,multiple: true },
                        category_id: "",

                    })
                    // 监听 category_id 变化
                    watch(() => state.category_id, (newVal, oldVal) => {

                        // 这里可以执行一些逻辑，例如重新获取数据

                        $('#c-category_id').val(newVal);
                    })
                    onMounted(() => {


                    })

                    function change(e) {
                        console.log(e)
                    }
                    return {
                        state,
                        change,

                    }
                }
            }
            createApp('app', addEdit);


        },

        add2: function() {
            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });

            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            });

            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });


        },

        add3: function() {
            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });

            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            });

            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });


        },
        edit: function() {




            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                //Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };



                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });

            console.log(from_specData)
            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            }, from_specData);




            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });

            const { reactive, computed, onMounted, getCurrentInstance, watch } = Vue
            const addEdit = {
                setup() {
                    const state = reactive({
                        options: Config.typeList,
                        props: { value: 'id', label: "name", children: "childlist",multiple: true },
                        category_id: Config.row.category_id,

                    })
                    // 监听 category_id 变化
                    watch(() => state.category_id, (newVal, oldVal) => {

                        // 这里可以执行一些逻辑，例如重新获取数据

                        $('#c-category_id').val(newVal);
                    })
                    onMounted(() => {
                        $('#c-category_id').val(Config.row.category_id);

                    })

                    function change(e) {
                        console.log(e)
                    }
                    return {
                        state,
                        change,

                    }
                }
            }
            createApp('app', addEdit);




        },

        edit2: function() {
            //Controller.api.bindevent();

            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                //Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });
            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            }, from_specData);

            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });
        },
        edit3: function() {
            //Controller.api.bindevent();

            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                //Fast.api.close(data);
                Toastr.success("商品提交成功");
            }, function(data, ret) {
                Toastr.success("商品提交失败");
            }, function(success, error) {
                //注意如果我们需要阻止表单，可以在此使用return false;即可
                //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                var form = this;
                if (form.size() === 0) {
                    Toastr.error("表单未初始化完成,无法提交");
                    return false;
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.size() > 0) {
                    var postFields = form.serializeArray().map(function(obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function(i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                var dataParam = { spec_many: specMany.getData() };
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : '') + (Object.keys(dataParam).length > 0 ? '&' + $.param(dataParam) : ''),
                    dataType: 'json',
                    complete: function(xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']", form).val(token);
                        }
                        //关闭弹窗
                        var index = parent.Layer.getFrameIndex(window.name);
                        var callback = parent.$("#layui-layer" + index).data("callback");
                        parent.Layer.close(index);
                        //刷新列表
                        parent.$("#table").bootstrapTable('refresh');
                    }
                }, function(data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']", form).val(data.token);
                        }
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                }, function(data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']", form).val(data.token);
                    }
                });
                return false;
            });
            // 注册商品多规格组件
            var specMany = new GoodsSpec({
                container: '.goods-spec-many',
                OutForm: Form
            }, from_specData);

            // 切换单/多规格
            $('select[name="row[spec_type]"').change(function(e) {
                var $goodsSpecMany = $('.goods-spec-many'),
                    $goodsSpecSingle = $('.goods-spec-single');
                if (e.currentTarget.value === '10') {
                    $goodsSpecMany.hide() && $goodsSpecSingle.show();
                } else {
                    $goodsSpecMany.show() && $goodsSpecSingle.hide();
                }
            });
        },
        select: () => {
            const { reactive, onMounted, watch, getCurrentInstance, nextTick } = Vue
            const select = {
                setup() {
                    const { proxy } = getCurrentInstance();

                    const state = reactive({
                        data_type: new URLSearchParams(location.search).get('data_type'),
                        multiple: new URLSearchParams(location.search).get('multiple') || false,
                        max: new URLSearchParams(location.search).get('max') || 0,
                        ids: new URLSearchParams(location.search).get('ids'), // 选中的商品ids
                        goods_ids: new URLSearchParams(location.search).get('goods_ids'), // 需要搜索的商品id列表
                        data: [],
                        filter: {
                            drawer: false,
                            data: {
                                category_ids: '',
                                keyword: '',
                                price: {
                                    min: '',
                                    max: '',
                                },
                            },
                            tools: {},
                        },
                    })
                    state.ids = state.ids ? state.ids.split(',') : []

                    const category = reactive({
                        id: '',
                        select: [],
                        detail: []
                    })

                    function getCategoryDetail() {
                        const data = category.select.find((item) => item.id == category.id);
                        category.detail = data.children || [];
                        state.filter.data.category_ids = data.id;
                    }

                    function changeCategoryIds(data) {
                        state.filter.data.category_ids = data.id;
                    }

                    function getData() {
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));

                        // activity_type 搜索
                        if (state.activity_type) {
                            tempSearch.activity_type = state.activity_type;
                        }
                        // id 搜索
                        if (state.goods_ids) {
                            tempSearch.goods = { field: 'id', value: state.goods_ids };
                        }
                        let search = composeFilter(tempSearch, {
                            keyword: 'like',
                            price: 'between',
                        });
                        Fast.api.ajax({
                            url: 'litestore/litestoregoods/select',
                            type: 'GET',
                            data: {
                                data_type: state.data_type,
                                type: 'page',
                                page: pagination.page,
                                list_rows: pagination.list_rows,
                                ...search,
                            },
                        }, function(ret, res) {
                            state.data = res.data.data
                            pagination.total = res.data.total

                            nextTick(() => {
                                state.data.forEach((l) => {

                                    if (state.ids?.includes(l.goods_id + '')) {
                                      proxy.$refs['multipleTableRef']?.toggleRowSelection(l, true);
                                      toggleRowSelection('row', [l], l);
                                    }

                                });
                            });
                            return false
                        }, function(ret, res) {})
                    }

                    watch(() => state.filter.data, () => {
                        pagination.page = 1;
                        getData()
                    }, {
                        deep: true,
                    })

                    const pagination = reactive({
                        page: 1,
                        list_rows: 10,
                        total: 0,
                    })

                    function onSelect(selection, row) {
                        if (
                            !state.max ||
                            (state.max && state.max > state.ids.length)
                        ) {
                            if (state.ids.includes(row.goods_id + '')) {
                                let index = state.ids.findIndex((id) => id == row.goods_id);
                                state.ids.splice(index, 1);
                            } else {
                                state.ids.push(row.goods_id);
                            }
                        }
                        toggleRowSelection('row', selection, row);
                    }

                    function onSelectAll(selection) {
                        if (
                            !state.max ||
                            (state.max && state.max > state.ids.length + selection.length)
                        ) {
                            if (selection.length == 0) {
                                state.data.forEach((l) => {
                                    if (state.ids.includes(l.goods_id)) {
                                        let index = state.ids.findIndex((id) => id == l.goods_id);
                                        state.ids.splice(index, 1);
                                    }
                                });
                            } else {
                                state.data.forEach((l) => {
                                    if (!state.ids.includes(l.goods_id)) {
                                        state.ids.push(l.goods_id);
                                    }
                                });
                            }
                        }
                        toggleRowSelection('all', selection);
                    }

                    function toggleRowSelection(type, selection, row) {
                      // 限制数量
                      if (state.max && state.max < selection.length) {
                        if (type === 'row') {
                          proxy.$refs['multipleTableRef']?.toggleRowSelection(row, false);
                        } else if (type === 'all') {
                          proxy.$refs['multipleTableRef']?.clearSelection();
                          state.data.forEach((l) => {
                            if (state.ids?.includes(l.id)) {
                              proxy.$refs['multipleTableRef']?.toggleRowSelection(l, true);
                            }
                          });
                        }
                        ElMessage({
                          type: 'warning',
                          message: '已到选择上限',
                        });
                        return false;
                      }
                    }

                    function onSingleSelect(item) {
                        Fast.api.close(item)
                    }

                    function onConfirm() {
                        let ids = state.ids.join(',')
                        Fast.api.ajax({
                            url: 'litestore/litestoregoods/select',
                            type: 'GET',
                            data: {
                                type: 'select',
                                search: JSON.stringify({ id: [ids, 'in'] })
                            },
                        }, function(ret, res) {
                            Fast.api.close(res.data)
                            return false
                        }, function(ret, res) {})
                    }

                    onMounted(() => {
                        getData()
                    })

                    return {
                        state,
                        category,
                        getCategoryDetail,
                        getData,
                        changeCategoryIds,
                        pagination,
                        onSelect,
                        onSelectAll,
                        onSingleSelect,
                        onConfirm
                    }
                }
            }
            createApp('select', select);
        },
        md: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});